package com.google.guava.concurrent;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author fuc
 * @create 2020-10-12
 */
public class ListenableFutureExample {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
    ExecutorService service = Executors.newFixedThreadPool(2);

    CompletableFuture<Integer> future =
        CompletableFuture.supplyAsync(
            () -> {
              try {
                TimeUnit.SECONDS.sleep(5);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              return 100;
            },
            service);
    future.whenComplete(
        (v, t) -> {
          System.out.println("I am finished and the result is " + v);
        });
  }
}
